using System.Collections.Generic;
using BCW.Friends;
using UnityEngine;

public class ListPlayersPVP : MonoBehaviour
{
	public List<butScrollPlayers> butPlayersList = new List<butScrollPlayers>();

	public Color firstPlaceColor;

	public Color secondPlaceColor;

	public Color thirdPlaceColor;

	public Color myPlayerBGColor;

	private Color normalBGColor;

	public UISprite backgroundSprite;

	public Color teamNoneColor;

	public Color teamRedColor;

	public Color teamBlueColor;

	public Color teamZombieColor;

	public GameObject playerButton;

	public UIGrid greed;

	public UIScrollView parentSV;

	public UIWidget widget;

	[HideInInspector]
	public bool active;

	private bool initialized;

	private PunTeams.Team team;

	private void Awake()
	{
		if (parentSV == null)
		{
			parentSV = base.transform.GetComponentInChildren<UIScrollView>();
		}
		if (widget == null)
		{
			widget = base.transform.GetComponentInChildren<UIWidget>();
		}
		if (greed == null)
		{
			greed = base.transform.GetComponentInChildren<UIGrid>();
		}
	}

	private void OnEnable()
	{
		CreatePlayer();
	}

	public void Init(bool myTeam)
	{
		team = ((!myTeam) ? TDMController.enemyTeam : TDMController.myTeam);
		if (team != 0)
		{
			if (GameController.instance.curTypeGame == TypeGame.Zombe)
			{
				if (TDMController.myTeam == PunTeams.Team.blue)
				{
					backgroundSprite.color = ((!myTeam) ? teamZombieColor : teamBlueColor);
				}
				else
				{
					backgroundSprite.color = ((!myTeam) ? teamBlueColor : teamZombieColor);
				}
			}
			else
			{
				backgroundSprite.color = ((!myTeam) ? teamRedColor : teamBlueColor);
			}
		}
		else
		{
			backgroundSprite.color = teamNoneColor;
		}
		initialized = true;
	}

	public void CreatePlayer()
	{
		if (!initialized || !active)
		{
			return;
		}
		List<PlayerBehavior> list = new List<PlayerBehavior>();
		if (GameController.instance.curTypeGame == TypeGame.normal)
		{
			list.AddRange(GameController.instance.listPlayers);
		}
		else
		{
			list.AddRange(GameController.instance.listPlayers.FindAll((PlayerBehavior x) => x.localTeam == team));
		}
		TeamExtensions.BubbleSort(list, TeamExtensions.SortPlayersByPoints);
		while (butPlayersList.Count > list.Count)
		{
			Object.Destroy(butPlayersList[butPlayersList.Count - 1].gameObject);
			butPlayersList.RemoveRange(butPlayersList.Count - 1, 1);
		}
		while (butPlayersList.Count < list.Count)
		{
			GameObject gameObject = Object.Instantiate(playerButton);
			gameObject.SetActive(true);
			gameObject.name = ((butPlayersList.Count >= 10) ? string.Empty : "0") + butPlayersList.Count;
			butScrollPlayers component = gameObject.GetComponent<butScrollPlayers>();
			butPlayersList.Add(component);
			gameObject.transform.SetParent(greed.transform);
			gameObject.transform.localScale = Vector3.one;
			normalBGColor = component.bg.color;
			gameObject.SetActive(true);
		}
		for (int i = 0; i < butPlayersList.Count; i++)
		{
			PlayerBehavior playerBehavior = list[i];
			butPlayersList[i].gameObject.name = ((i >= 10) ? string.Empty : "0") + i;
			butPlayersList[i].labelNomPlayers.text = string.Empty + (i + 1);
			butPlayersList[i].labelPointsPlayers.text = string.Empty + playerBehavior.Points;
			butPlayersList[i].labelNamePlayers.text = string.Empty + playerBehavior.props.nickname;
			butPlayersList[i].idPlayer = playerBehavior.props.playerId;
			bool flag = false;
			if (BCWServer.idPlayer == butPlayersList[i].idPlayer)
			{
				butPlayersList[i].ChangeTexture(FriendsButton.Me);
			}
			else
			{
				foreach (string friendsId in FriendsController.instance.FriendsIds)
				{
					if (friendsId == butPlayersList[i].idPlayer)
					{
						butPlayersList[i].ChangeTexture(FriendsButton.Friend);
						flag = true;
					}
				}
				foreach (string listSendedId in FriendsUIGame.instance.listSendedIds)
				{
					if (listSendedId == butPlayersList[i].idPlayer)
					{
						butPlayersList[i].ChangeTexture(FriendsButton.Sended);
						flag = true;
					}
				}
				if (!flag)
				{
					if (FriendsUIGame.instance.CanSendInvite())
					{
						butPlayersList[i].ChangeTexture(FriendsButton.Plus);
					}
					else
					{
						butPlayersList[i].ChangeTexture(FriendsButton.InactiveFriend);
					}
				}
			}
			if (i + 1 <= 3)
			{
				switch (i)
				{
				case 0:
					butPlayersList[i].labelNomPlayers.color = firstPlaceColor;
					break;
				case 1:
					butPlayersList[i].labelNomPlayers.color = secondPlaceColor;
					break;
				case 2:
					butPlayersList[i].labelNomPlayers.color = thirdPlaceColor;
					break;
				}
			}
			if (playerBehavior == PlayerBehavior.MyPlayer)
			{
				butPlayersList[i].bg.color = myPlayerBGColor;
			}
			else
			{
				butPlayersList[i].bg.color = normalBGColor;
			}
			butPlayersList[i].GetComponent<UIDragScrollView>().scrollView = parentSV;
		}
		CancelInvoke("Reposition");
		Invoke("Reposition", 0.1f);
	}

	public void Show(bool val)
	{
		if (!active)
		{
			widget.alpha = 0f;
		}
		else
		{
			widget.alpha = (val ? 1 : 0);
		}
	}

	private void Reposition()
	{
		greed.repositionNow = true;
	}

	public void RemovePlayers()
	{
		foreach (butScrollPlayers butPlayers in butPlayersList)
		{
			Object.Destroy(butPlayers.gameObject);
		}
		butPlayersList.Clear();
	}
}
